package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseServiceTypeAddBo;
import com.manage.modules.bo.TBaseServiceTypeQueryBo;
import com.manage.modules.bo.TBaseServiceTypeEditBo;
import com.manage.modules.domain.TBaseServiceType;
import com.manage.modules.mapper.TBaseServiceTypeMapper;
import com.manage.modules.vo.TBaseServiceTypeVo;
import com.manage.modules.service.ITBaseServiceTypeService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 维护类型Service业务层处理
 *
 * @author wx
 * @date 2021-08-17
 */
@Service
public class TBaseServiceTypeServiceImpl extends ServiceImpl<TBaseServiceTypeMapper, TBaseServiceType> implements ITBaseServiceTypeService {

    @Override
    public TBaseServiceTypeVo queryById(Long serviceTypeId) {
        TBaseServiceType db = this.baseMapper.selectById(serviceTypeId);
        return BeanUtil.toBean(db, TBaseServiceTypeVo.class);
    }

    @Override
    public List<TBaseServiceTypeVo> queryList(TBaseServiceTypeQueryBo bo) {
        LambdaQueryWrapper<TBaseServiceType> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getServiceTypeName()), TBaseServiceType::getServiceTypeName, bo.getServiceTypeName());
        lqw.eq(bo.getServiceTypeState() != null, TBaseServiceType::getServiceTypeState, bo.getServiceTypeState());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseServiceTypeVo> entity2Vo(Collection<TBaseServiceType> collection) {
        List<TBaseServiceTypeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseServiceTypeVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseServiceType> page = (Page<TBaseServiceType>) collection;
            Page<TBaseServiceTypeVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseServiceTypeAddBo bo) {
        TBaseServiceType add = BeanUtil.toBean(bo, TBaseServiceType.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseServiceTypeEditBo bo) {
        TBaseServiceType update = BeanUtil.toBean(bo, TBaseServiceType.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseServiceType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
